<template>
  <el-form
    :model="fromData"
    ref="ruleFormRef"
    label-width="auto"
    label-position="left"
    class="loginFrom"
    hide-required-asterisk
    :rules="rules"
  >
    <el-form-item label="用户名" prop="username">
      <el-input 
        type="text" 
        v-model="fromData.username" 
        placeholder="请输入账号"
        size="default"
      />
    </el-form-item>
    <el-form-item label="密码" prop="password">
      <el-input 
        type="password" 
        v-model="fromData.password" 
        placeholder="请输入密码"
        size="default"
        show-password
      />
    </el-form-item>
    <el-form-item label="验证码" prop="code">
      <el-row :gutter="5">
        <el-col :span="15">
          <el-input 
            type="text" 
            v-model="fromData.code" 
            placeholder="请输入验证码"
            size="default"
          />
        </el-col>
        <el-col :span="9">
          <div class="captcha-container">
            <img 
              :src="captchaUrl" 
              alt="验证码" 
              @click="refreshCaptcha"
              class="captcha-image"
            />
            <el-icon class="refresh-icon" @click="refreshCaptcha">
              <Refresh />
            </el-icon>
          </div>
        </el-col>
      </el-row>
    </el-form-item>
    <el-row :gutter="20" class="remember-forgot">
      <el-col :span="12" style="text-align: left">
        <el-checkbox v-model="rememberMe">记住我</el-checkbox>
      </el-col>
      <el-col :span="12" style="text-align: right">
        <el-link type="primary" @click="handleForgotPassword">忘记密码</el-link>
      </el-col>
    </el-row>
  </el-form>
</template>

<script setup lang="ts">
import { reactive, ref, onMounted } from 'vue'
import { Refresh } from '@element-plus/icons-vue'
import type { FormInstance } from 'element-plus'

const rememberMe = ref(false)
const ruleFormRef = ref<FormInstance>()
const captchaUrl = ref('')

const fromData = reactive({
  username: '',
  password: '',
  code: '',
})

const rules = {
  username: [
    {
      required: true,
      message: '请输入用户名',
      trigger: ['blur', 'change'],
    },
    {
      min: 3,
      max: 20,
      message: '用户名长度在 3 到 20 个字符',
      trigger: ['blur', 'change'],
    }
  ],
  password: [
    { 
      required: true, 
      message: '请输入密码', 
      trigger: ['blur', 'change'] 
    },
    {
      min: 6,
      max: 20,
      message: '密码长度在 6 到 20 个字符',
      trigger: ['blur', 'change'],
    }
  ],
  code: [
    { 
      required: true, 
      message: '请输入验证码', 
      trigger: ['blur', 'change'] 
    },
    {
      len: 4,
      message: '验证码长度为 4 位',
      trigger: ['blur', 'change'],
    }
  ]
}

// 定义 emit 事件
const emit = defineEmits(['submitForm'])

// 暴露给父组件的方法
const validateForm = async (): Promise<boolean> => {
  if (!ruleFormRef.value) return false
  
  try {
    await ruleFormRef.value.validate()
    emit('submitForm', { ...fromData, rememberMe: rememberMe.value })
    return true
  } catch (error) {
    return false
  }
}

const refreshCaptcha = () => {
  // 这里应该调用后端接口获取新的验证码
  captchaUrl.value = `/api/captcha?t=${Date.now()}`
}

const handleForgotPassword = () => {
  // 处理忘记密码
  console.log('忘记密码')
}

// 组件挂载时获取验证码
onMounted(() => {
  refreshCaptcha()
})

// 暴露方法给父组件
defineExpose({
  validateForm
})
</script>

<style scoped lang="less">
.loginFrom {
  .remember-forgot {
    margin-top: 10px;
  }

  .captcha-container {
    position: relative;
    height: 32px;
    border-radius: 4px;
    overflow: hidden;
    cursor: pointer;
    transition: all 0.3s ease;

    &:hover {
      transform: scale(1.02);
    }

    .captcha-image {
      width: 100%;
      height: 100%;
      object-fit: cover;
      border-radius: 4px;
    }

    .refresh-icon {
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);
      color: #fff;
      font-size: 14px;
      opacity: 0;
      transition: opacity 0.3s ease;
      background: rgba(0, 0, 0, 0.5);
      border-radius: 50%;
      padding: 3px;
    }

    &:hover .refresh-icon {
      opacity: 1;
    }
  }
}

:deep(.el-form-item) {
  margin-bottom: 20px;

  .el-form-item__label {
    font-weight: 500;
    color: #333;
  }
}

:deep(.el-checkbox) {
  .el-checkbox__label {
    color: #666;
  }
}

:deep(.el-link) {
  font-size: 14px;
  
  &:hover {
    color: #667eea;
  }
}
</style>
